package com.csy.feign.user.fallback;

import java.util.Collections;

import org.springframework.cloud.openfeign.FallbackFactory;

import com.csy.commons.constant.HttpStatus;
import com.csy.commons.core.page.PageResult;
import com.csy.commons.dto.SysRole;
import com.csy.commons.dto.SysUser;
import com.csy.commons.vo.AjOwnerVo;
import com.csy.feign.user.api.UserServiceApi;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UserServiceApiFallbackFactory implements FallbackFactory<UserServiceApi> {

	@Override
	public UserServiceApi create(Throwable cause) {
		// 1. 毫无疑问，进入这里肯定是抛异常了，打印错误日志以便后面调试错误
		log.error("> [UserServiceApi]接口调用异常(进入降级处理): {}, ", cause.getMessage());
		
		// 2. 为了不影响业务正常链路，尽量返回一个结果，比如返回一个空数组等等。
		return new UserServiceApi() {

			@Override
			public PageResult list(SysRole role) {
				PageResult pageResult = new PageResult();
		        pageResult.setCode(HttpStatus.SUCCESS);
		        pageResult.setRows(Collections.EMPTY_LIST);
		        pageResult.setTotal(0L);
				return pageResult;
			}

			@Override
			public SysUser getByName(String nickName) {
				return null;
			}

			@Override
			public PageResult list(SysUser user) {
				PageResult pageResult = new PageResult();
		        pageResult.setCode(HttpStatus.SUCCESS);
		        pageResult.setRows(Collections.EMPTY_LIST);
		        pageResult.setTotal(0L);
				return pageResult;
			}

			@Override
			public SysUser getByUsername(String username) {
				return null;
			}

			@Override
			public PageResult list(AjOwnerVo ajOwnerVo) {
				PageResult pageResult = new PageResult();
		        pageResult.setCode(HttpStatus.SUCCESS);
		        pageResult.setRows(Collections.EMPTY_LIST);
		        pageResult.setTotal(0L);
				return pageResult;
			}

			@Override
			public AjOwnerVo getOwnerByName(String ownerRealName) {
				return null;
			}};
	}

}
